<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="赛事" prop="matchId">
        <el-select v-model="dataForm.matchId" placeholder="请选择赛事" style="width: 300px" @change="getMatchItemList" disabled>
          <el-option v-for="item in matchList" :key="item.id" :label="item.matchName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地区" prop="province">
        <el-select style="width: 285px" placeholder="请选择" v-model="dataForm.province" @change="selectCity" disabled>
          <el-option :value="selectedIndex" label="全国">全国</el-option>
          <el-option v-for="itemO in area.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
        ~
        <el-select style="width: 285px" placeholder="请选择" v-model="dataForm.city">
          <el-option :value="selectedIndex" label="全市">全部</el-option>
          <el-option v-for="itemO in areaCity.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="赛项" prop="matchItemId">
        <el-select v-model="dataForm.matchItemId" placeholder="赛项" style="width: 300px">
          <el-option v-for="item in matchItemList" :key="item.id" :label="item.baseMatchItemName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
export default defineComponent({
  props: { paramsId: String, areaProvinceCode: String },
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        matchId: "",
        province: "",
        city: "",
        matchItemId: "",
        creator: "",
        createDate: ""
      },
      matchList: [],
      matchItemList: [],
      area: { code: "0", item: [{ code: "0", name: "请选择" }] },
      areaCity: { code: "0", item: [{ code: "0", name: "请选择" }] },
      selectedIndex: "-1"
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },

  created: function () {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },

  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.areaList(this.area);
          this.getInfo();
        }
      });

      this.$nextTick(() => {
        this.dataForm.matchId = this.paramsId;
        this.dataForm.province = this.areaProvinceCode;
        this.selectCity();
      });
      this.getMatch();
      this.areaList(this.area);
      this.getMatchItemList();
    },
    // 获取信息
    getInfo() {
      baseService.get("/sp/spmatcharea/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
        this.dataForm.id = res.data.id + "";
        this.dataForm.matchId = res.data.matchId + "";
        this.dataForm.matchItemId = res.data.matchItemId + "";
        this.selectCity();
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/sp/spmatcharea", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              //this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    },

    /**
     * 查询赛事
     */
    getMatch() {
      baseService.get("/sp/spmatch/list").then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.matchList = res.data;
      });
    },

    /**
     * 查询赛项
     */
    getMatchItemList() {
      if (!this.dataForm.matchId) {
        return;
      }
      baseService.get("/sp/spmatchitem/list?matchId=" + this.dataForm.matchId).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.matchItemList = res.data;
      });
    },

    /**
     * 获取省份
     * @param target
     */
    areaList(target: any) {
      baseService.get("/front/fast/sys/areaDict/list?pid=" + target.code).then((e: any) => {
        target.item = e.data;
      });
    },

    /**
     * 选择城市
     */
    selectCity() {
      if (this.dataForm.province) {
        this.areaCity.code = this.dataForm.province;
        this.areaList(this.areaCity);
      } else {
        this.dataForm.city = "";
        this.areaCity.item = [];
      }
    }
  }
});
</script>
